package com.briup.mt.service.impl;

import com.briup.mt.bean.BaseUser;
import com.briup.mt.bean.BaseUserExample;
import com.briup.mt.dao.BaseUserMapper;
import com.briup.mt.service.IBaseUserService;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class BaseUserServiceImpl implements IBaseUserService {

    @Resource
    private BaseUserMapper baseUserMapper;

    @Override
    public List<BaseUser> query(String username, String gender,String address) {
        BaseUserExample example = new BaseUserExample();

        if(!StringUtils.isEmpty(username)){
            example.createCriteria().andUsernameLike("%"+username+"%");
        }
        if(!StringUtils.isEmpty(gender)){
            example.createCriteria().andGenderEqualTo(gender);
        }

        if(!StringUtils.isEmpty(address)){
            example.createCriteria().andAddressLike("%"+address+"%");
        }


        List<BaseUser> list = baseUserMapper.selectByExample(example);
        return list;
    }

    @Override
    public void saveOrUpdate(BaseUser baseUser) {
        // 如果userInfo中存在id属性，表示更新；否则保存
        if (baseUser.getId()==null){
            baseUserMapper.insert(baseUser);
        }else {
            baseUserMapper.updateByPrimaryKey(baseUser);
        }

    }

    @Override
    public void deleteById(long id) {
        baseUserMapper.deleteByPrimaryKey(id);
    }
}
